#NoEnv
#Include Crypt.ahk
#Include CryptConst.ahk
#Include CryptFoos.ahk

str_magic = __cr_magic__%COMPUTERNAME%

    ; Spreadsheetの設定をmvc iniファイルから取得する
    ; (設定機能はなし)
    mvinifile = mvcap.ini
    IniRead, GoogleSpr_User,        %mvinifile%, Coord, GoogleSpr_User
    IniRead, GoogleSpr_Pass,        %mvinifile%, Coord, GoogleSpr_Pass
    IniRead, GoogleSpr_SheetName,   %mvinifile%, Coord, GoogleSpr_SheetName

    if (GoogleSpr_User == "ERROR"
        || GoogleSpr_Pass == "ERROR"
        || GoogleSpr_SheetName == "ERROR" ) {
        Msgbox, お手数ですが、mvc.exeでアカウント設定してからご使用ください。
        return
    }
    GoogleSpr_User := Crypt.Encrypt.StrDecrypt(GoogleSpr_User, str_magic, 5, 1) ;  decrypts the string using previously generated hash,AES_128 and MD5
    GoogleSpr_Pass := Crypt.Encrypt.StrDecrypt(GoogleSpr_Pass, str_magic, 5, 1) ;  decrypts the string using previously generated hash,AES_128 and MD5
    if ( GoogleSpr_User == ""
        || GoogleSpr_Pass == ""
        || GoogleSpr_SheetName == "" ) {
        Msgbox, お手数ですが、mvc.exeでアカウント設定してからご使用ください。
        return
    }

    Gui, Add, Text,         x12  y20 w100 h20 , サーバ(&V)
    Gui, Add, ListBox,      x12  y40 w100 h40 vServer, 指定なし||EMERALD|DIAMOND|PEARL

    Gui, Add, Text,         x132 y20 w150 h20 , アイテム名(&I)
    Gui, Add, ComboBox,     x132 y40 w150 h20 vItemName, 

    Gui, Add, Text,         x452 y20 w300 h20 , 価格上限(&P) (マイナスの場合:下限 範囲の場合: 100-1000)
    Gui, Add, ComboBox,     x452 y40 w150 h20 vPrice, 100|200|5000|1000000
    Gui, Add, Edit,         x452 y60 w250 h20 vQueryDebug hwndHQueryDebug, (クエリー)

    Gui, Add, Button,       x622 y40 w60 h20 Default gBtnSearch vBtnS hwndHBtnS, 検索(&S)
    Gui, Add, Button,       x690 y40 w60 h20 gBtnRel, リロード(&R)

    Gui, Add, Edit,         x12  y85 w1200 h540 vEditResult,

    Gui, Add, Text,         x12  y640 w420 h30 , シート: %GoogleSpr_SheetName%

    ; Generated using SmartGUI Creator 4.0
    Gui, Show, h700 w1230, mvc Query: Google SpreadSheetクエリー
Return  ; AutoExec section end

GuiClose:
ExitApp

BtnRel:
F12::
    Reload
return

BtnSearch:
    Gui, Submit, Nohide
    server_query =
    QueryDebug =
    if (Server && Server != "指定なし" ) {
        server_query = server == %Server%
        QueryDebug = %server_query%
    }
    if (Price) {
        Price := RegExReplace(Price, "[^0-9\-=]", "")
        if (Price > 0) {
            if (RegExMatch(Price, "[0-9]+\-[0-9]+")) {
                price_query := RegExReplace(Price, "([0-9]+)\-([0-9]+)", "price > ${1} && price < ${2}")
            } else {
                Price := RegExReplace(Price, "[^0-9\-]", "")
                price_query = price < %Price%
            }
        } else {
            Price2 := abs(Price)
            price_query = price > %Price2%
        }
        if (server_query && price_query) {
            QueryDebug .= " && "
        }
        QueryDebug .= price_query
    }
    GuiControl, , QueryDebug, %QueryDebug%
    GuiControl, , Price, %Price%

    ToolTip, 実行中・・・
    GuiControl, Disable, %HBtnS%
    cmd = %ComSpec% /c python mvc_query.py %GoogleSpr_User% %GoogleSpr_Pass% "%GoogleSpr_SheetName%" "%QueryDebug%"
    OutputDebug, %cmd%
    RunWait, %cmd%,,
    GuiControl, Enable, %HBtnS%
    ToolTip,

    Header = 日付	サーバ	エリア	最寄りアルター	番地	X座標	Z座標	価格	品名`n
    FileEncoding, UTF-8
    if (!ItemName) {
        FileRead, EditResult, query_output.txt
        EditResult := Header . EditResult
    } else {
        buffer := Header
        Loop, Read, query_output.txt
        {
            ; あいまい検索(スペースは抜く)
            target := RegExReplace(A_LoopReadLine, "[ -@/]", "")
            needle := RegExReplace(ItemName, "[ -@/]", "")
            if (InStr(target, needle)) {
                buffer .= A_LoopReadLine "`n"
            }
        }
        EditResult := buffer
    }

    GuiControl, , EditResult, %EditResult%
return
